home *** CD-ROM | disk | FTP | other *** search
- Path: sun001.spd.dsccc.com!sun001!dthornto
- From: dthornto@aplo266.spd.dsccc.com (Duane Thornton)
- Newsgroups: comp.lang.c
- Subject: Re: HELP NEEDED: What the hell is wrong with my program?
- Date: 11 Jan 1996 18:57:03 GMT
- Organization: none
- Message-ID: <DTHORNTO.96Jan11125703@aplo266.spd.dsccc.com>
- References: <4d1qmp$h43@carbon.cudenver.edu>
- NNTP-Posting-Host: aplo266.spd.dsccc.com
- In-reply-to: exli@ouray.cudenver.edu's message of 11 Jan 1996 01:55:37 GMT
-
- >>>>> On 11 Jan 1996 01:55:37 GMT, exli@ouray.cudenver.edu (ELLIE XIAO-YU LI) said:
-
- ELLIE> first of all, i have to thank all of you who have answered
- ELLIE> me through email or have followed up my post. and here is
- ELLIE> more details about my problem.
-
- ELLIE> the system i am using is a dec osf/1. the code is
- ELLIE> something like:
-
- ELLIE> #include <string.h>
-
- ELLIE> typedef struct {
- ELLIE> .
- ELLIE> .
- ELLIE> .
- ELLIE> char *name;
- ELLIE> .
- ELLIE> .
- ELLIE> .
- ELLIE> } aStruct;
-
- ELLIE> aStruct *ptr;
- ELLIE> char *str1="this is a test";
- ELLIE> char *str2="this is another test";
-
- ELLIE> ptr=(aStruct *)malloc(sizeof(aStruct));
- ptr-> name=(char *)malloc(strlen(str1));
-
- ^^^^^This should be strlen(str1 + 1)
- The length returned by 'strlen' does not include the terminator
- (\0). Therefore the following 'strcpy' actually copies the terminator
- outside of the memory reserved by the above 'malloc'.
-
- ELLIE> strcpy(ptr->name, str1); /*no problem here*/
- ELLIE> ...
- ELLIE> /*then later i want ptr->name to point to another string*/
- ELLIE> free(ptr->name);
- ptr-> name=(char *)malloc(strlen(str2));
- ELLIE> strcpy(ptr->name, str2); /*problem arised here*/
-
-
- [snip]
-
- ELLIE> Mitch
- ELLIE> exli@ouray.cudenver.edu
-
- Duane
- --
- Duane Thornton x94916
-
- ---------------------------- Duane Thornton --------------------------------
- Internet: dthornto@spd.dsccc.com Opinions are my own.
- DSC Communications Corporation Addr: MS 153, 1000 Coit Rd, Plano, TX 75075
- ----------------------------------------------------------------------------
-